package com.dk.shared.proxy;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @Description
 * @Author 王大凯
 * @Date 2024-01-18 11:03
 */
public class ProxyApiProxyFactory<T> implements FactoryBean<T> {

    private Class<T> interfaceClass;
    private InvocationHandler handler;


    public ProxyApiProxyFactory(Class<T> interfaceClass, InvocationHandler handler) {
        this.interfaceClass = interfaceClass;
        this.handler = handler;
    }

    @Override
    public T getObject() throws Exception {
        return (T) Proxy.newProxyInstance(
                interfaceClass.getClassLoader(),
                new Class[]{interfaceClass},
                handler
        );
    }

    @Override
    public Class<?> getObjectType() {
        return interfaceClass;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}